package com.trailwalker.traceability.net.interceptor;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.trailwalker.JavaUtils.log.LogUtils;
import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.traceability.entitiy.application.GlobalData;
import com.trailwalker.traceability.net.CompanyNetworkManager;
import com.trailwalker.traceability.util.GsonUtils;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.DecimalFormat;

public class CompanyInterceptor implements okhttp3.Interceptor {

    private static final String NEED_TOKEN_VALUE = "false";
    private static final String AUTHORIZATION = "Authorization";
    private static final String NEED_TOKEN_STR = "needToken";
    private static final String FOR_NAME = "UTF-8";

    private static final String TAG = CompanyInterceptor.class.getSimpleName();

    private static final Charset UTF8 = Charset.forName(FOR_NAME);
    private final GlobalData mGlobalData;

    private boolean isShowNetLog = false;

    public static CompanyInterceptor create(GlobalData globalData) {
        return new CompanyInterceptor(globalData);
    }

    private CompanyInterceptor(GlobalData globalData) {
        mGlobalData = globalData;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //LogUtils.log(TAG, "url >> " + request.url());
        Request.Builder requestBuilder = request.newBuilder();
        String needTokenStr = request.header(NEED_TOKEN_STR);
        boolean needToken = true;
        if (!StringUtils.isEmpty(needTokenStr)
                && NEED_TOKEN_VALUE.equals(needTokenStr)) {
            needToken = false;
        }
        if (needToken) {
            String token = mGlobalData.getString("token", "");
            LogUtils.log(TAG, token);
            request = requestBuilder
                    .addHeader(AUTHORIZATION, "Bearer " + token)
                    .removeHeader(NEED_TOKEN_STR)
                    .build();
        } else {
            LogUtils.log(TAG, "no token");
            request = requestBuilder
                    .removeHeader(NEED_TOKEN_STR)
                    .build();
        }
        Response originalResponse = chain.proceed(request);
        ResponseBody responseBody = originalResponse.body();
        BufferedSource source = responseBody.source();
        source.request(Long.MAX_VALUE);
        Buffer buffer = source.buffer();
        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            charset = contentType.charset(UTF8);
        }
        String bodyString = buffer.clone().readString(charset);
        return originalResponse.newBuilder()
                .body(ResponseBody.create(contentType, bodyString))
                .build();
    }
}